//
//  ItemRestaurantExtension.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ItemRestaurantExtension.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"
#import "ItemExtension.h"
#import "Item.h"

@implementation ItemRestaurant(Extension)

+ (BOOL) initItemRestaurant{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return NO;
	}
	NSMutableArray * itemResArray = [[[NSMutableArray alloc] init] autorelease];
	NSMutableDictionary *itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:1] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:2] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:12] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:2] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:4] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:20] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:3] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:3] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:18] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:4] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:1] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:19] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:5] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:5] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:5] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:6] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:6] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:2] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:7] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:7] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:9] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:8] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:8] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:10] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:9] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:9] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:15] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:10] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:10] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:13] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:11] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:11] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:6] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:12] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:12] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:2] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:13] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:13] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:21] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:14] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:14] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:22] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:15] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:15] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:3] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:16] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:16] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:4] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:17] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:17] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:23] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:18] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:18] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:14] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:19] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:19] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:16] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:20] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:20] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:17] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	itemResDict = [[NSMutableDictionary alloc] init];
	[itemResDict setObject:[NSNumber numberWithInt:21] forKey:@"id"];
	[itemResDict setObject:[NSNumber numberWithInt:21] forKey:@"itemId"];
	[itemResDict setObject:[NSNumber numberWithInt:24] forKey:@"restaurantId"];
	[itemResArray addObject:itemResDict];
	[itemResDict release];
	
	for (int i = 0; i < [itemResArray count]; i++) {
		NSMutableDictionary *dict = [itemResArray objectAtIndex:i];
		ItemRestaurant *itemRestaurant = [NSEntityDescription insertNewObjectForEntityForName:@"ItemRestaurant" inManagedObjectContext:managedObjectContext];
		itemRestaurant.id = [dict objectForKey:@"id"];
		itemRestaurant.itemId = [dict objectForKey:@"itemId"];
		itemRestaurant.restaurantId = [dict objectForKey:@"restaurantId"];
	}
	[managedObjectContext save:&err];
	return YES;
		
}

+ (NSArray *) getItemRestaurants{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"ItemRestaurant" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}

+ (ItemRestaurant *) getItemRestaurantWithId:(int) itemRestaurantId{
	ItemRestaurant* itemRestaurant = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *itemRestaurantArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", itemRestaurantId];	
	@try {
		itemRestaurantArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"ItemRestaurant" :predicate :nil :NO :managedObjectContext];
		
		if ([itemRestaurantArray count] != 0)
		{
			itemRestaurant = [itemRestaurantArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return itemRestaurant;
}

+ (NSArray *) getItemRestaurantWithRestaurant:(int) restaurantId{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *itemRestaurantArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"restaurantID = %d", restaurantId];	
	@try {
		itemRestaurantArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"ItemRestaurant" :predicate :nil :NO :managedObjectContext];
		
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	return itemRestaurantArray;
}

+ (NSArray *) getItemRestaurantWithItem:(int) itemId{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *itemRestaurantArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"itemID = %d", itemId];	
	@try {
		itemRestaurantArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"ItemRestaurant" :predicate :nil :NO :managedObjectContext];
		
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return itemRestaurantArray;
}

@end
